package com.example.stgv;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ChangePin extends Activity {

	TextView txtPinOld, txtPinNew, txtPinConfirm;
	private DBHelper db;
	PinData pins = new PinData();
	Button btnHuy, btnDoi;
	String pinOld, pinNew, pinConfirm, pinOld1;
	int cpin;
	Boolean kt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_pin);

		txtPinOld = (TextView) findViewById(R.id.txtPinCu);
		txtPinNew = (TextView) findViewById(R.id.txtPinMoi);
		txtPinConfirm = (TextView) findViewById(R.id.txtPinXacNhan);

		db = new DBHelper(ChangePin.this);
		Cursor cur = db.runAllDateRQ("select mapin from pin;");
		cpin = cur.getCount();
		Log.d("Thong Bao", "Dem record" + cpin);
		if (cur == null || cpin == 0) {
			txtPinOld.setEnabled(false);
			kt = false;
			cur.close();
		} else {
			kt = true;
			txtPinOld.requestFocus();
			cur.moveToFirst();
			Log.d("Thong Bao", cur.getString(0));
			pinOld1 = cur.getString(0);
			
		}

		btnHuy = (Button) findViewById(R.id.btn_Change_Pin_Cancel);
		btnHuy.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});

		btnDoi = (Button) findViewById(R.id.btn_Change_Pin_Add);
		btnDoi.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (checkData()) {
					UpdatePin();
					finish();
				}
			}
		});
	}

	public Boolean checkData() {

		try {
			pinNew = txtPinNew.getText().toString().trim();
			pinConfirm = txtPinConfirm.getText().toString().trim();
			pinOld = db.convertMD5(txtPinOld.getText().toString().trim());

			if ((txtPinOld.isEnabled() == true && pinOld.isEmpty())
					|| pinNew.isEmpty() || pinConfirm.isEmpty()) {
				Toast.makeText(this, "Không được để trống.", Toast.LENGTH_SHORT)
						.show();
				return false;
			}
			if (!pinNew.equals(pinConfirm)) {
				Log.d("Thong Bao", "Ma pin khong giong " + pinNew + " cf: "
						+ pinConfirm);
				Toast.makeText(this, "Xác nhận Pin không giống.",
						Toast.LENGTH_SHORT).show();
				return false;
			}

			if (kt && !pinOld1.equals(pinOld)) {
				Log.d("Thong Bao", "Sai pin cu " + pinOld1 + " New: " + pinOld);
				Toast.makeText(this, "Sai mã PIN cũ", Toast.LENGTH_SHORT).show();
				return false;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	private Boolean UpdatePin() {
		try {
			
			int nParam = 1;
			String[] fields = new String[nParam];
			String[] values = new String[nParam];

			fields[0] = pins.getKEY_MAPIN();

			values[0] = db.convertMD5(pinNew);
//			values[0] = pinNew;
			
			if (cpin > 0) {

				long cur1 = db.UpdateData(pins.getDATABASE_TABLE_PIN(), 1, fields,values,null,null);	
				Log.d("Thong Bao", "Update mapin values ('" + pinNew
						+ "') " + cur1);
			} else {
				long cur1 = db.insertData(pins.getDATABASE_TABLE_PIN(), 1, fields,
						values);			
				Log.d("Thong Bao", "insert into pin (mapin) values ('" + pinNew
						+ "') ");
			}
			db.close();

			Toast.makeText(this, "Đã cập nhật mã PIN.", Toast.LENGTH_SHORT)
					.show();
			return true;

		} catch (Exception e) {
			Log.d("Update", "Khong the Update. ");
			Toast.makeText(this, "Khong the doi.", Toast.LENGTH_SHORT).show();
			return false;
		}
	}
}
